##################################################################################################################################
#
# Copyright (C) 2008-2013 Mind System Connection. Lda. Part of Daidze Software, created by Siqsuruq. All other rights reserved.
#
# Mind System Connection Webpage: www.msysc.org
# Daidze Software Page: www.daidze.com www.daidze.org
# E-mail: admin@msysc.org, siqsuruq@gmail.com
#
##################################################################################################################################

proc add_user {} {
	set values [$::daidze_main get_values]
	if {[dict get $values username] == ""} {
		tk_messageBox -icon error -type ok -message "[::msgcat::mc "Username is obligatory. Enter username."]"
	} elseif {[dict get $values user_group] == ""} {
		tk_messageBox -icon error -type ok -message "[::msgcat::mc "Group is obligatory. Choose usergroup."]"
	} else {
		set user_data [dict create name [dict get $values username] password [md5::md5 -hex \"[dict get $values password]\"] email [dict get $values email] email_password [dict get $values email_password] email_server [dict get $values email_server] email_port [dict get $values email_port]]
		$::db transaction start
		$::db insert_all daidze_user $user_data
		set user_id [$::db select_last_inserted daidze_user id]
		set group_id [$::db select_id_by_name daidze_user_level [dict get $values user_group]]
		set query "INSERT INTO \"daidze_user_permission\" (fk_daidze_user,fk_daidze_user_level) VALUES ($user_id,$group_id)"
		$::db execute_query $query
		$::db transaction stop
		$::daidze_main load_main daidze_users users.xml $::main 1
	}
}

proc delete_user {table_var} {
	set my_tablelist [dict get [$::daidze_main get_fdata] $table_var]

	if {[$my_tablelist curselection] ne ""} {
		set row [$my_tablelist cellcget [$my_tablelist curselection],0 -text]
		if {$row == 1} {
			tk_messageBox -icon error -type ok -message "[::msgcat::mc "This is the first user in the system. You cant delete it."]"
		} else {
			$::db transaction start
			$::db delete_row daidze_user $row
			set query "DELETE FROM daidze_user_permission WHERE fk_daidze_user=\'$row\'"
			$::db execute_query $query
			$::db transaction stop
			$::daidze_main load_main daidze_users users.xml $::main 1
		}
	} else {
		tk_messageBox -icon info -type ok -message "[::msgcat::mc "Select entry to edit."]"
	}
}

proc edit_user {table_var} {
	set sdata [dict create]
	set my_tablelist [dict get [$::daidze_main get_fdata] $table_var]
	set user_var {id name email user_group}
	if {[$my_tablelist curselection] ne ""} {
	set row [$my_tablelist rowcget [$my_tablelist curselection] -text]
		foreach key $user_var value $row {
			dict set sdata $key $value
		}
		dict set sdata password [lindex [$::db select_all daidze_user password daidze_user.id=\'[dict get $sdata id]\' list] 0]
		dict set sdata email_password [lindex [$::db select_all daidze_user email_password daidze_user.id=\'[dict get $sdata id]\' list] 0]
		dict set sdata email_server [lindex [$::db select_all daidze_user email_server daidze_user.id=\'[dict get $sdata id]\' list] 0]
		dict set sdata email_port [lindex [$::db select_all daidze_user email_port daidze_user.id=\'[dict get $sdata id]\' list] 0]
		$::daidze_main load_main daidze_users edit_user.xml $::main
		$::daidze_main update_form $sdata
	} else {
		tk_messageBox -icon info -type ok -message "[::msgcat::mc "Select entry to edit."]"
	}
}

proc update_user {} {
	set values [$::daidze_main get_values]
	if {[dict get $values name] == ""} {
		tk_messageBox -icon error -type ok -message "[::msgcat::mc "Username is obligatory. Enter username."]"
	} elseif {[dict get $values user_group] == ""} {
		tk_messageBox -icon error -type ok -message "[::msgcat::mc "Group is obligatory. Choose usergroup."]"
	} else {
		set user_data [dict create id [dict get $values id] name [dict get $values name] password [md5::md5 -hex \"[dict get $values password]\"] email [dict get $values email] email_password [dict get $values email_password] email_server [dict get $values email_server] email_port [dict get $values email_port]]
		$::db transaction start
		$::db update_all daidze_user $user_data
		set group_id [$::db select_id_by_name daidze_user_level [dict get $values user_group]]
		set query "UPDATE daidze_user_permission SET fk_daidze_user_level=$group_id WHERE fk_daidze_user='[dict get $values id]'"
		$::db execute_query $query
		$::db transaction stop
		$::daidze_main load_main daidze_users users.xml $::main 1
	}
}